<template>
  <div class="test"><van-button @click="login">login</van-button></div>
</template>

<script setup>
import { TOKEN_KEY } from "@/constants";
import { setStore } from "@/utils/storage";

const router = useRouter();
const route = useRoute();

const login = () => {
  showLoadingToast({
    message: "加载中...",
    forbidClick: true,
    duration: 0
  });

  setTimeout(() => {
    closeToast();
    setStore(TOKEN_KEY, "token");
    const { path, queryParams } = parseRedirect();
    router.push({ path: path, query: queryParams });
  }, 3000);
};

function parseRedirect() {
  const query = route.query;
  const redirect = query.redirect || "/";

  const url = new URL(redirect, window.location.origin);
  const path = url.pathname;
  const queryParams = {};

  url.searchParams.forEach((value, key) => {
    queryParams[key] = value;
  });

  return { path, queryParams };
}
</script>

<style lang="less" scoped>
.test {
  @apply flex justify-center;
}
</style>
